// pages/erweima/erweima.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    geterweimaurl: app.data.url + '/mapi/newfenxiao/getaccesstoken',
    erweima:false,
    touxiang:'',
    name:'',
    canvas:false,
    canvasWidth: 0,
    canvasHeight: 0,
    canvas:true,
  },



  asdddd: function (context, x, y, r, stepClear) {
    var calcHeight = r - stepClear;
    var calcWidth = Math.sqrt(r * r - calcHeight * calcHeight);

    var rectWidth = r - calcWidth;
    var rectheight = stepClear;
    if (rectWidth > 0.1) {
      context.fillRect(x - r, y - r, rectWidth, rectheight);
      context.fillRect(x + calcWidth, y - r, rectWidth, rectheight);
      context.fillRect(x - r, y + r - stepClear, rectWidth, rectheight);
      context.fillRect(x + calcWidth, y + r - stepClear, rectWidth, rectheight);
      stepClear += 0.5;
      this.asdddd(context, x, y, r, stepClear);
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  zhiixng: function () {
    //that.setData({canvas:true})
    var myCanvasWidth;
    var myCanvasHeight;
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        myCanvasWidth = res.windowWidth
        myCanvasHeight = res.windowHeight

      },
    })
    this.setData({
      canvasWidth: myCanvasWidth,
      canvasHeight: myCanvasHeight

    })
    // 使用 wx.createContext 获取绘图上下文 context
    var asd = this.data.canvasWidth; // 屏幕的宽度
    var het = this.data.canvasHeight; // 屏幕的宽度
    var context = wx.createCanvasContext('firstCanvas');
    var touxiang = this.data.touxiang;
    var erweima = that.data.erweima;
    console.log(touxiang);
    console.log(erweima);
    wx.downloadFile({
      url: touxiang,
      success:function(res){
        touxiang = res.tempFilePath;
        wx.downloadFile({
          url: erweima,
          success:function(res){
            erweima = res.tempFilePath;
            //设置背景颜色
            context.setFillStyle('#FFA660');
            context.fillRect(0, 0, asd, het);


            //头像生成
            context.drawImage(touxiang, 100, 230, 60, 60);
            context.setFillStyle('#FFA660');
            that.asdddd(context, 130, 260, 30, 0.5);

            //第一块图片
            var path = '../../assets/shangyizhang.png';
            context.drawImage(path, 0, 0, asd, 220)


            // //绘制上面的图片
            setTimeout(function () {
              wx.canvasToTempFilePath({
                canvasId: 'firstCanvas',
                success: function (res) {
                  var tempFilePath = res.tempFilePath;
                  // console.log(tempFilePath);
                  wx.setStorageSync('img', tempFilePath)
                  return tempFilePath
                },
                fail: function (res) {
                  console.log(res);
                }
              });
            }, 200);

            var text = '我是' + that.data.name;//这是要绘制的文本
            context.setFontSize(18);
            context.setFillStyle("#FFFFFF");
            context.fillText(text, 180, 250, 200, 500);

            var textd = '快来一起赚钱吧！';//这是要绘制的文本
            context.setFontSize(18);
            context.setFillStyle("#FFFFFF");
            context.fillText(textd, 180, 280, 200, 500);

            //二维码
            context.drawImage(erweima, 80, 310, 200, 200);
            var textdd = '扫描二维码，和我一起赚钱';//这是要绘制的文本
            context.setFontSize(18);
            context.setFillStyle("#FFFFFF");
            context.fillText(textdd, 80, 560);
            context.draw()
            // that.setData({canvas:true});
            wx.hideLoading();
          }
        })
      }
    })
  
  },

  shengcheng:function(){
    //this.zhiixng();   //画布函数
    console.log('haha');
    wx.canvasToTempFilePath({
      canvasId: 'firstCanvas',
      success: function (res) {
        var tempFilePath = res.tempFilePath;
        // console.log(tempFilePath);
        // wx.setStorageSync('img', tempFilePath)
        // return tempFilePath
        console.log(tempFilePath);
        wx.saveImageToPhotosAlbum({
          filePath: tempFilePath,
          success:function(){
            wx.showToast({
              title: '保存成功',
            })
          }
        })
      },
      fail: function (res) {
        console.log(res);
      }
    })
  },




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // var that = this;
    // var user = wx.getStorageSync('user');
    // // wx.showLoading({
    // //   title: '加载中',
    // // })
    // wx.request({
    //   url: that.data.geterweimaurl,
    //   data:{token:user.token},
    //   method:'post',
    //   success:function(res){
    //     var data = res.data;
    //     console.log(data);
    //     if(data.code==200){
    //       // console.log(data.data);
    //       that.setData({erweima:data.data});
    //     }else{
    //       wx.showToast({
    //         title: data.msg,
    //         icon:'none'
    //       })
    //     }
    //     // wx.hideLoading();
    //   }
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

    // this.zhiixng();   //画布函数
    //this.zhiixng();
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var user = wx.getStorageSync('user');
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: that.data.geterweimaurl,
      data: { token: user.token },
      method: 'post',
      success: function (res) {
        var data = res.data;
        console.log(data);
        if (data.code == 200) {
          // console.log(data.data);
          that.setData({ erweima: data.data });
          wx.getUserInfo({
            success: function (e) {
              // console.log(e);
              that.setData({ touxiang: e.userInfo.avatarUrl, name: e.userInfo.nickName })
              that.zhiixng();
            }
          })



        } else {
          wx.showToast({
            title: data.msg,
            icon: 'none'
          })
        }
        // wx.hideLoading();
      }
    })


    // var that = this;
    // wx.getUserInfo({
    //   success:function(e){
    //     // console.log(e);
    //     that.setData({ touxiang: e.userInfo.avatarUrl,name:e.userInfo.nickName})
    //     that.zhiixng();
    //   }
    // })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var user = wx.getStorageSync('user');
    var titles = wx.getStorageSync('titles');
    return {
      title: titles,
      path: '/pages/index/index?scene' + user.id,
    };
  }
})
